<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class ProjectMall extends Model
{
    protected $table = "project_mall";
    public $timestamps = false;
    public $guarded = [];

    public function businessLicenseImage(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => is_null($value) ? null : json_decode($value,true),
            set: fn ($value) => is_array($value) ? json_encode($value) : $value,
        );
    }

    public function otherFileUrl(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => is_null($value) ? null : json_decode($value,true),
            set: fn ($value) => is_array($value) ? json_encode($value) : $value,
        );
    }

    public function project(): BelongsTo
    {
        return $this->belongsTo(Project::class,"project_id","id");
    }

    public function mall(): BelongsTo
    {
        return $this->belongsTo(Mall::class,"merchant_no","merchant_no");
    }

    function capitalRel()
    {
        return $this->hasMany(ProjectMallCapital::class,"p_mall_id","id");
    }

    public function capital(): HasManyThrough
    {
        return $this->hasManyThrough(
            Capital::class,
            ProjectMallCapital::class,
            "p_mall_id",
            "id",
            "id",
            "capital_id",
        );
    }
}
